//
//  LKTitleScrollView.m
//  YLHelper
//
//  Created by she1323 on 2018/5/24.
//  Copyright © 2018年 she1323. All rights reserved.
//

#import "LKTitleScrollView.h"

@interface LKTitleScrollView()<UIScrollViewDelegate>

@property(nonatomic,strong)UIScrollView *titleScrollView;

@property(nonatomic,assign)NSInteger currentIndex;
@end


@implementation LKTitleScrollView

-(RACSubject *)subject{
    if (!_subject) {
        _subject = [RACSubject subject];
    }
    return _subject;
}

-(instancetype)initWithFrame:(CGRect)frame AndTitlesArray:(NSArray *)titlesArray{
    if (self = [super init]) {
        self.titlesArray = titlesArray;
        self.frame = frame;
        [self setDataBtn];
    }
    return self;
}

-(void)setDataBtn{
    self.currentIndex = 0;
    [self addSubview:self.titleScrollView];
    for (NSInteger i = 0; i < self.titlesArray.count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(self.titleScrollView.centerX - 15 + 30 * i, 0, 30 , 44);
        if (i == self.currentIndex) {
            btn.titleLabel.font = [UIFont systemFontOfSize:16];
            btn.selected = YES;
        }else{
            btn.titleLabel.font = [UIFont systemFontOfSize:14];
        }
        [btn setTitleColor:COLOR_HEX(0x90B5CF) forState:UIControlStateNormal];
        [btn setTitleColor:COLOR_HEX(0xFFFFFF) forState:UIControlStateSelected];
        btn.tag = 10000 + i;
        [self.titleScrollView addSubview:btn];
        [btn setTitle:self.titlesArray[i] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    }
}
-(void)btnAction:(UIButton *)btn{
    btn.selected = !btn.selected;
    btn.titleLabel.font = [UIFont systemFontOfSize:17];
    NSInteger number = btn.tag - 10000;
    if (number == self.currentIndex) {
        
    }else{
        UIButton *sbtn = (UIButton *)[self.titleScrollView viewWithTag:self.currentIndex + 10000];
        sbtn.selected = NO;
        sbtn.titleLabel.font = [UIFont systemFontOfSize:14];
        self.currentIndex = number;
    }
    [self.titleScrollView setContentOffset:CGPointMake(30 * number, 0) animated:YES];
    NSLog(@"点击的%ld",number);
    [self.subject sendNext:self.titlesArray[number]];
}
-(UIScrollView *)titleScrollView{
    if (!_titleScrollView) {
        _titleScrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
        _titleScrollView.contentSize = CGSizeMake(self.bounds.size.width * 2, self.bounds.size.height);
        _titleScrollView.delegate = self;
        _titleScrollView.showsHorizontalScrollIndicator = NO;
        _titleScrollView.showsHorizontalScrollIndicator = NO;
        _titleScrollView.panGestureRecognizer.enabled = NO;
        
        for (NSInteger i = 0; i < _titleScrollView.gestureRecognizers.count; i++) {
            UIGestureRecognizer *ges = _titleScrollView.gestureRecognizers[i];
            [_titleScrollView removeGestureRecognizer:ges];
        }
    }
    return _titleScrollView;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
}
@end
